<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/banner-list' }">轮播图管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加轮播图</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-upload
        action="/upload"
        name="pic"
        :limit="1"
        list-type="picture-card"
        :on-success="handleSuccess"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <el-button type="success" @click="insert()">添加轮播图</el-button>
  </div>
</template>

<script>
export default {
  name: "BannerAddNew",
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      url: ""
    }
  }, methods: {
    insert() {
      if (this.url == "") {
        this.$message.error("请选择上传的图片!");
        return;
      }
      //发出添加轮播图的请求
      axios.get("/banner/insert?url=" + this.url).then(function (response) {
        //显示到管理页面
        location.href = "/admin.html";
      })
    },
    handleSuccess(response, file, fileList) {
      //response代表服务器响应的图片路径
      this.url = response;
    },
    goBack() {
      history.back();//返回上一页面
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      //发出删除图片的请求
      axios.get("/remove?url=" + this.url).then(function () {
        this.$message("服务器图片删除完成");
        this.url = "";
      })
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  }
}
</script>

<style scoped>

</style>